package com.zhida.courseservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhida.common.context.UserContext;
import com.zhida.common.entity.Result;
import com.zhida.courseservice.entity.Activity;
import com.zhida.courseservice.entity.Course;
import com.zhida.courseservice.mapper.ActivityMapper;
import com.zhida.courseservice.service.IActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import static com.zhida.common.utils.RegexUtil.ROOT_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ling
 * @since 2024-07-17
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Resource
    ActivityMapper activityMapper;

    @Override
    public Result<?> createActivity(Activity activity) {
        if (!ROOT_KEY.equals(UserContext.getUser())){
            return Result.error403("无权限");
        }
        activity.setActivityOwner(UserContext.getUser());
//        插入数据
        int result = activityMapper.insert(activity);
        if (result == 1)
            return Result.successMsg("发布活动成功");
        else
            return Result.error("发布活动失败");
    }

    @Override
    public Result<?> getActivityInfo(Integer activity_id) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id",activity_id);
        Activity activity = activityMapper.selectOne(queryWrapper);
        if (activity != null){
            return Result.success(activity);
        }
        return Result.error404("未查询到该课程信息");
    }

    @Override
    public Result<?> updateActivityInfo(Activity activity) {
//        判断是否有权限更改
        if (!ROOT_KEY.equals(UserContext.getUser())){
            return Result.error403("无权限操作课程更新");
        }
        if (activityMapper.updateById(activity) == 1){
            return Result.successMsg("更新成功");
        }
        return Result.error404("更新失败");
    }

    @Override
    public Result<?> getActivities(Integer current,Integer pageSize) {
        Page<Activity> page = new Page<>(current,pageSize);
        return Result.success(activityMapper.selectPage(page,null));
    }
}
